import requests
import os

def download_image(url):
    folder = "imgdown"
    base_filename = "generated_image"
    extension = ".png"
    
    # 创建文件夹如果不存在
    if not os.path.exists(folder):
        os.makedirs(folder)

    # 生成不重复的文件名
    i = 1
    while True:
        filename = f"{base_filename}{i}{extension}"
        save_path = os.path.join(folder, filename)
        if not os.path.exists(save_path):
            break
        i += 1

    # 下载并保存图片
    response = requests.get(url)
    if response.status_code == 200:
        with open(save_path, 'wb') as f:
            f.write(response.content)
        print(f"图片已保存到 {save_path}")
    else:
        print(f"下载失败，状态码：{response.status_code}")

if __name__ == "__main__":
    #Just a test , you can replace it with your own url to test the function of download_image()
    url = ""
    download_image(url)